package cn.zero.service.impl;

import cn.zero.domain.Product;
import cn.zero.domain.PurchaseApply;
import cn.zero.domain.Stock;
import cn.zero.domain.StockWarningLog;
import cn.zero.dto.StockDto;
import cn.zero.exception.GlobalCustomException;
import cn.zero.fegin.ProductFegin;
import cn.zero.fegin.PurchaseFegin;
import cn.zero.mapper.StockMapper;
import cn.zero.properties.StockProperties;
import cn.zero.result.JSONResult;
import cn.zero.service.IStockService;
import cn.zero.service.IStockWarningLogService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Tbs
 * @since 2022-09-29
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements IStockService {
    
    @Autowired
    private ProductFegin productFegin;
    @Autowired
    private StockProperties stockProperties;
    @Autowired
    private IStockWarningLogService stockWarningLogService;
    @Autowired
    private PurchaseFegin purchaseFegin;


    @Override
    public void iao(StockDto stockDto) {
        Stock stock = selectStock(stockDto.getProductId());
        if (stockDto.getStatus() == 0){ //入库

            if (stock==null){ //如果为null就新增
                stock = new Stock();
                stock.setCount(stockDto.getCount());
                stock.setProductId(stockDto.getProductId());

                //调用服务查询商品
                JSONResult jsonResult = productFegin.get(stockDto.getProductId());
                //内容在data内，转换json格式
                String jsonString = JSONObject.toJSONString(jsonResult.getData());
                //转成product
                Product product = JSONObject.parseObject(jsonString, Product.class);

                stock.setProductName(product.getProductName());
                stock.setCreateTime(new Date());
                //新增
                insert(stock);
            } else { //不为null就增加count
                stock.setCount(stock.getCount() + stockDto.getCount()); // 存在修改数量
                updateById(stock);
            }


        }else { //出库

            if (stock.getCount()< stockProperties.getMin()){//库存不足
                StockWarningLog stockWarningLog = new StockWarningLog();//报警日志

                BeanUtils.copyProperties(stock,stockWarningLog); // 使用复制进stockwarninglog
                stockWarningLog.setCreateTime(new Date());
                stockWarningLog.setInfo(stock.getProductName() + "库存不足，请及时添加！");
                //添加日志
                stockWarningLogService.insert(stockWarningLog);

                getIndent(stock);

            }else {//库存充足
                stock.setCount(stock.getCount()-stockDto.getCount());
                insert(stock);
                //生成采购单
                getIndent(stock);
            }
        }
    }

    @Override
    public List<Stock> lack() {
        // 1.获取库存阈值
        Integer threshold = stockProperties.getMin();
        Wrapper<Stock> wrapper = new EntityWrapper<>();
        wrapper.lt("count", threshold);
        List<Stock> stocks = selectList(wrapper);
        return stocks;
    }

    private void getIndent(Stock stock) {
        //生成采购单
        PurchaseApply purchaseApply = new PurchaseApply();
        //工具类复制
        BeanUtils.copyProperties(stock,purchaseApply);
        purchaseApply.setCount(100);
        purchaseApply.setStatus(0); // 待审核
        purchaseApply.setCreateTime(new Date());
        purchaseFegin.saveOrUpdate(purchaseApply);
        throw new GlobalCustomException("库存不足，出库失败");
    }

    private Stock selectStock(Long productId) {
        Wrapper<Stock> wapper = new EntityWrapper<>();
        wapper.eq("product_id",productId);
        return selectOne(wapper);
    }


}
